(function(global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
    typeof define === 'function' && define.amd ? define(['jquery'], factory) :
    factory($, CodeMirror);
}(this, function($, CodeMirror) {
  'use strict';

  function updatePreview(dframe, content) {
    var preview = dframe.contentDocument || dframe.contentWindow.document;
    preview.open();
    preview.write(content);
    preview.close();
  }
  $(function() {
    $('.runjs').each(function() {
      var div = document.createElement("div");
      div.setAttribute("class", "docs-preview");
      var dframe = document.createElement("iframe");
      div.appendChild(dframe);
      $(this).parent().append(div);
      var editor = CodeMirror.fromTextArea($(this)[0], {
        mode: 'text/html',
        lineNumbers: true
      });
      editor.on("change", function() {
        updatePreview(dframe, editor.getValue());
      });
      updatePreview(dframe, editor.getValue());
    })
  })

}))
